package db;

import beans.B_CriterioAuditoria;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import beans.B_CriterioPlanAuditoria;
import conexion.Conexion;
import java.sql.Types;

/**
 *
 * @author JoseHernan
 */
public class DB_CriterioPlanAuditoria {

    public static ArrayList<B_CriterioPlanAuditoria> getCriterioAuditoria(int id) {
        ArrayList<B_CriterioPlanAuditoria> list = new ArrayList<B_CriterioPlanAuditoria>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;

            ps = con.prepareStatement(" select id, id_criterio_auditoria from tbl_criterio_plan_auditoria where id = " + id);

            rs = ps.executeQuery();
            B_CriterioPlanAuditoria obj;
            B_CriterioAuditoria objCriterioAuditoria;
            while (rs.next()) {
                obj = new B_CriterioPlanAuditoria();
                objCriterioAuditoria = new B_CriterioAuditoria();
                //Asign values
                obj.setId(rs.getInt("id"));
                objCriterioAuditoria.setId(rs.getInt("id_criterio_auditoria"));
                obj.setObjCriterioAuditoria(objCriterioAuditoria);
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    public static ArrayList<B_CriterioPlanAuditoria> getListByIdPlanAuditoria() {
        ArrayList<B_CriterioPlanAuditoria> list = new ArrayList<B_CriterioPlanAuditoria>();
        try {
            Connection con = Conexion.getConexion();

            PreparedStatement ps;
            ResultSet rs;

            ps = con.prepareStatement(" select cpa.id, cpa.id_criterio_auditoria, ca.nombre as criterio_auditoria from tbl_criterio_plan_auditoria cpa inner join tbl_plan_auditoria pa on pa.id = cpa.id_plan_auditoria inner join tbl_criterio_auditoria ca on ca.id = cpa.id_criterio_auditoria where pa.removed = false and pa.is_active = true and cpa.removed = false ");

            rs = ps.executeQuery();
            B_CriterioPlanAuditoria obj;
            B_CriterioAuditoria objCriterioAuditoria;
            while (rs.next()) {
                obj = new B_CriterioPlanAuditoria();
                objCriterioAuditoria = new B_CriterioAuditoria();
                //Asign values
                obj.setId(rs.getInt("id"));
                objCriterioAuditoria.setId(rs.getInt("id_criterio_auditoria"));
                objCriterioAuditoria.setNombre(rs.getString("criterio_auditoria"));
                obj.setObjCriterioAuditoria(objCriterioAuditoria);
                //Add to List
                list.add(obj);
            }
            rs.close();
            ps.close();
            con.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return list;
    }

    //SP CRUD
    public static String crud(B_CriterioPlanAuditoria obj, String action) {
        String result = "";
        CallableStatement cs = null;
        Connection con = Conexion.getConexion();
        try {
            cs = con.prepareCall("{call sp_crud_criterio_plan_auditoria(?,?,?,?,?,?)}");

            cs.registerOutParameter("p_message", Types.VARCHAR);
            cs.setString("p_action", action);
            cs.setInt("p_id", obj.getId());
            cs.setInt("p_id_criterio_auditoria", obj.getObjCriterioAuditoria().getId());
            cs.setInt("p_id_plan_auditoria", obj.getObjPlanAuditoria().getId());
            cs.setString("p_user", obj.getUser());
            cs.executeUpdate();
            result = cs.getString(1);
        } catch (Exception ex) {
            result = ex.getMessage();
        } finally {
            try {
                cs.close();
                con.close();
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        return result;
    }
}
